#include <cstdio>
#include <cctype>
#include <cstring>

// Largest roman number: MMMMDCCCLXXXVIII
#define MAX_LENGTH 17

typedef char symbol;


const symbol pos_symbol[][3] = {
		{'I', 'V', 'X'},
		{'X', 'L', 'C'},
		{'C', 'D', 'M'},
		{'M', ' ', ' '}
};


struct symbol_info
{
	int value, pos;
};

symbol_info roman[256];


inline void init ()
{
	roman['I'].pos = 1; roman['I'].value = 1;
	roman['V'].pos = 1; roman['V'].value = 5;
	roman['X'].pos = 2; roman['X'].value = 10;
	roman['L'].pos = 2; roman['L'].value = 50;
	roman['C'].pos = 3; roman['C'].value = 100;
	roman['D'].pos = 3; roman['D'].value = 500;
	roman['M'].pos = 4; roman['M'].value = 1000;
}


/************************************************/


struct string
{
	symbol
		str[MAX_LENGTH],
		*last;

	inline symbol & operator * () {return *str;}
	inline symbol & operator [] (size_t n) {return str[n];}

	inline void operator += (const string &s) {
		const symbol *c = s.str;
		while (c < s.last) {
			*last++ = *c++;
		}
		*last = '\0';
	}

	inline void operator += (const symbol *s) {
		while (*s) {
			*last = *s;
			++last;
			++s;
		}
		*last = '\0';
	}

	inline void reset () {
		*str = '\0';
		last = str;
	}

	inline int readline () {
		reset ();
		while (*last = getchar (), isalnum (*last))
			++last;
		*last = '\0';
		return last - str;
	}

	inline bool is_decimal_number () {
		return isdigit (*str);
	}

	inline bool is_roman_number () {
		return isalpha (*str);
	}
};


/************************************************/


inline void parse_to_roman (const symbol n, symbol *&out, int pos)
{

	const symbol *set = pos_symbol[pos];

	switch (n)
	{
		case '1':
			*out++ = set[0];
			break;
		case '2':
			*out++ = set[0];
			*out++ = set[0];
			break;
		case '3':
			*out++ = set[0];
			*out++ = set[0];
			*out++ = set[0];
			break;
		case '4':
			*out++ = set[0];
			*out++ = set[1];
			break;
		case '5':
			*out++ = set[1];
			break;
		case '6':
			*out++ = set[1];
			*out++ = set[0];
			break;
		case '7':
			*out++ = set[1];
			*out++ = set[0];
			*out++ = set[0];
			break;
		case '8':
			*out++ = set[1];
			*out++ = set[0];
			*out++ = set[0];
			*out++ = set[0];
			break;
		case '9':
			*out++ = set[0];
			*out++ = set[2];
			break;
	}
	*out = '\0';
}


const symbol * decimal_to_roman (const string &str_in, string &str_out)
{
	int decimal_position;
	const symbol *c_in;

	str_out.reset ();
	c_in = str_in.str;
	decimal_position = str_in.last - str_in.str;

	while (decimal_position--) {
		parse_to_roman (*c_in, str_out.last, decimal_position);
		++c_in;
	}

	return str_out.str;
}


/************************************************/


inline int parse_to_decimal (const symbol *&in)
{
	int value, pos;
	symbol c;

	c = *in;
	if (c == '\0')
		return 0;

	++in;
	value = roman[c].value;
	pos = roman[c].pos;
	c = *in;
	if (c == '\0')
		return value;

	if (roman[c].value / value == 10 || roman[c].value / value == 5)
	{
		++in;
		return roman[c].value - value;
	}

	while (pos == roman[c].pos)
	{
		value += roman[c].value;
		++in;
		c = *in;
	}

	return value;
}


int roman_to_decimal (const string &str_in)
{
	int value;
	const symbol *s;

	s = str_in.str;
	value = 0;

	while (*s)
		value += parse_to_decimal (s);

	return value;
}


/************************************************/


int main ()
{
	string str_in, str_out;

	init ();

	while (str_in.readline ())
	{
		if (isdigit(*str_in))
			puts(decimal_to_roman(str_in, str_out));
		else
			printf("%d\n", roman_to_decimal(str_in));
	}

	return 0;
}